<template>
  <div class="app-container">

    <div class="filter-container" style="margin-bottom:10px">
      <el-input
        v-model="keywords"
        placeholder="关键字"
        style="width: 200px"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search(keywords)">搜索</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="openAdd"
      >新建</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="export2Excel"
      >导出Excel</el-button>
    </div>

    <el-table :data="list" border fit highlight-current-row>
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">{{ scope.$index }}</template>
      </el-table-column>
      <el-table-column label="ID" align="center">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="损坏设备名称" align="center">
        <template slot-scope="scope">{{ scope.row.deviceName }}</template>
      </el-table-column>
      <el-table-column label="设备ID" align="center">
        <template slot-scope="scope">{{ scope.row.deviceId }}</template>
      </el-table-column>
      <el-table-column label="型号" align="center">
        <template slot-scope="scope">{{ scope.row.modelId }}</template>
      </el-table-column>
      <el-table-column label="报修内容" align="center">
        <template slot-scope="scope">{{ scope.row.content }}</template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" width="175">
        <template slot-scope="scope">{{ scope.row.createdTime }}</template>
      </el-table-column>
      <el-table-column label="报修人" align="center">
        <template slot-scope="scope">{{ scope.row.userName }}</template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="openEdit(row.id)">
            修改
          </el-button>
          <el-button size="mini" type="danger" @click="isDelete(row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="display:flex">
      <el-pagination
        background
        layout="sizes, prev, pager, next, jumper, ->, total, slot"
        :total="total"
        @current-change="currentChange"
        @size-change="sizeChange"
      />
    </div></div>
</template>

<script>

export default {
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      size: 10,
      keywords: ''
    }
  },
  // 启动自动执行该方法
  created() {
    this.fetchData()
  },

  methods: {
    search(keywords) { // 查询方法
      alert(keywords)
      return this.list.filter(item => {
        if (item.name.includes(keywords)) {
          return item
          // eslint-disable-next-line no-unreachable
          alert(item)
        }
      })
    },
    export2Excel() {
      this.$confirm('是否导出该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        window.open('http://localhost:8080/repair/excelList')
        this.$message({
          type: 'success',
          message: '导出成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消！'
        })
      })
      // window.location.href = 'http://localhost:8080/repair/excelList' //  跳转链接
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    openAdd() {
      this.$router.push('/repair/addRepair')
    },
    openEdit(id) {
      this.$router.push({ name: 'EditRepair', params: { id: id }})
    },
    sizeChange(pageSize) {
      this.size = pageSize
      this.fetchData()
    },
    currentChange(currentPage) {
      this.page = currentPage
      this.fetchData()
    },
    async fetchData() {
      const { data: res } = await this.$axios.get('repair/getRepairList?page=' + this.page + '&size=' + this.size)
      // console.log(res)
      this.list = res.data
      this.total = res.total
      this.$message({
        type: 'success',
        message: '查询成功!'
      })
    },
    isDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.delete(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delete(id) {
      this.$axios.delete('repair/deleteRepair?id=' + id)
        .then(res => {
          this.fetchData()
        })
    }
  }
}
</script>
